/*
    * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
 */

#ifndef SHARE_SEND_MANAGER_H
#define SHARE_SEND_MANAGER_H

#include <string>
#include <map>
#include <vector>
#include <thread>
#include <mutex>

#include "file_share_queue.h"
#include "file_share_errors.h"
#include "file_share_log.h" 
#include "i_file_share_callback.h"

namespace OHOS {
namespace FileShare {
struct SessionInfo {
    int sessionId = -1;
    std::string networkId = "";
    bool sendFlag = false;
    ShareType type = ShareType::UNKNOWN;
    std::vector<FileInfo> files;
};

class SendManager {
public:
    static SendManager &GetInstance();

public:
    int32_t Init();
    int32_t DeInit();
    int32_t SendFile(SendTask &task);
    int32_t RegisterFileShareCallback(sptr<IFileShareCallback> callback);
    int32_t UnRegisterFileShareCallback();
    void NotifyEvent(int sessionId, SendType type, SendState state, int speed = 0, int progress = 0);
    void SetSendStopFlag(int sessionId);
    int32_t CancelSendFile(const std::string &deviceId);
    int32_t AbortReceiveFile(const std::string &deviceId);
    void UpdateFileInfo(int sessionId, const char *storagePath, const char *fileName, const char *newFileName);
    bool CheckFileHash(int sessionId);
    int32_t SetShareStatus(const bool status);   
    int32_t GetShareStatus(bool &status);
    bool AddSinkSessionInfo(const std::string &deviceId, SendTask &task);
    bool ReadySinkSession(const std::string &networkId, int sessionId);
    void RemoveSinkSessionInfo(int sessionId);
    int32_t RegisterShareStatusCallback(const std::string &bundleName, const sptr<IFileShareCallback> &callback);
    int32_t UnRegisterShareStatusCallback(const std::string &bundleName);
    int32_t ChangeUuid(const uint16_t uuid);
    bool HandleMediaAsset(int sessionId);

protected:
    void ProcessThread();

private:
    int32_t CreateSession();
    int32_t CheckFilePaths(std::vector<FileInfo> &files);
    int32_t GetNetWorkId(SendTask &task);
    int32_t OpenSessionAndSendFile(SendTask &task);
    std::string GetDeviceId(int sessionId);
    int32_t HashFile(std::vector<FileInfo> &files);
    int32_t SyncSendTask(SendTask task);
    void NotifyEventInner(SendState state, ShareType type); // source
    bool CopyFileToMediaAsset(const FileInfo &file, std::vector<std::string> &fileUris);
    SendManager() = default;
    ~SendManager() = default;
    SendManager(const SendManager &) = delete;
    SendManager &operator=(const SendManager &) = delete;
    SendManager(SendManager &&) = delete;
    SendManager &operator=(SendManager &&) = delete;

private:
    sptr<IFileShareCallback> callback_;
    std::map<std::string, SessionInfo> sessionInfoMap_send_;
    std::shared_ptr<FileShareQueue<SendTask>> sendQueue_ = nullptr;
    std::thread processThr_ = {};
    bool isStop_ = true;
    std::map<std::string, SessionInfo> sessionInfoMap_recv_;  // sink
    std::mutex shareStatusMutex_;
    bool shareStatus_ = false;
    std::map<std::string, sptr<IFileShareCallback>> shareStatusCallbackMap_;
    bool isRegister_;
};
} // namespace FileShare
} // namespace OHOS

#endif // SHARE_SEND_MANAGER_H